﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using Warehouse.API.Applcation.Command.Movelibrary;
using Warehouse.API.Applcation.Command.MovelibraryCommand;
using Warehouse.Domain;
using Warehouse.Infrstructre;
using Warehouse.Infrstructre.Interface;

namespace Warehouse.API.Applcation.CommandHandlers.MovelibraryCommandHandlers
{
    public class MovelibraryUpdCommandHandlers:IRequestHandler<MovelibraryUpdCommand, int>
    {
        protected readonly IEnterStoragesInfoRepsitory inforep;
        private readonly IMovelibraryRepository _movelibraryRep;
        private readonly IMoveProductRepository _moveProductRep;
        private readonly EFCoreDBcontext dbContext;

        public MovelibraryUpdCommandHandlers(IMovelibraryRepository movelibraryRep, IMoveProductRepository moveProductRep, EFCoreDBcontext dbContext,IEnterStoragesInfoRepsitory inforep)
        {
            _movelibraryRep = movelibraryRep;
            _moveProductRep = moveProductRep;
            this.dbContext = dbContext;
            this.inforep= inforep;
        }

        public async Task<int> Handle(MovelibraryUpdCommand request, CancellationToken cancellationToken)
        {
            using (var tran = await dbContext.Database.BeginTransactionAsync((System.Data.IsolationLevel.Serializable)))
            {

                try
                {
                    var se = (await _movelibraryRep.GetAsync(x => x.MovelibraryId == request.updDto.MovelibraryId)).FirstOrDefault();
                    se.MovelibraryCode = request.updDto.MovelibraryCode;
                    se.MovelibraryType = request.updDto.MovelibraryType;
                    se.MorderNumber = request.updDto.MorderNumber;
                    se.MakePerson = request.updDto.MakePerson;
                    se.MarkTime = request.updDto.MarkTime;
                    se.ORremark = request.updDto.ORremark;
                    se.ReasonForReview = request.updDto.ReasonForReview;
                    se.DocumentStatus = 1;
                    se.IsRemoved = false;
                    se.office_id = request.updDto.office_id;
                    se.user_id = request.updDto.user_id;


                    // 先处理 procurement1
                    var taskToAdd = await _movelibraryRep.UpdateAsync(se);
                    //查询移库产品信息
                    var infolist = (await _moveProductRep.GetAllAsyns()).Where(x => x.MovelibraryCode == request.updDto.MovelibraryCode);
                    //先删除原有移库产品信息
                    foreach (var item in infolist)
                    {
                        await _moveProductRep.RemoveAsync(item);
                    }
                    foreach (var item in request.updDto.updAllProducs)
                    {
                        MoveProduct detail = new MoveProduct
                        {
                            ProductsID = item.ProductsID,
                            MovelibraryCode = se.MovelibraryCode,
                            MoveProductNum = item.MoveProductNum,
                            Movelocation = item.Movelocation
                        };
                        await _moveProductRep.AddAsync(detail);
                    }

                    // 提交事务
                    await tran.CommitAsync();

                    return 1;  // 返回最终的结果
                }
                catch (Exception)
                {
                    // 回滚事务
                    await tran.RollbackAsync();
                    throw;
                }
            }
        }
    }
}
